﻿using ShunFengApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShunFengApi;
using ZBJF.Warranty.Common.Enums.SocketEnum;
using ZBJF.Warranty.Model.ViewModel.SocketModel;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.WebProvider;
using System.Web.Mvc;
using System.Transactions;
using ZBJF.Warranty.FengQiao;

namespace ZBJF.Warranty.CustomerService.Controllers
{
    public class ShunFengApiController : BaseController
    {
        //
        // GET: /ShunFengApi/
        #region 创建新的订单 + void AddNewOrder(OrderInfoModel model)  
        [HttpPost]
        [ValidateInput(true)]
        public void AddNewOrder(InsuranceWarrantyRecordExpressInfo insuranceWarrantyRecordExpressInfo,
            long insuranceWarrantyRecordId = 0)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = insuranceWarrantyRecordExpressInfo.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            var serv = ServiceFactory.InsuranceWarrantyRecordExpressInfoServiceInstance;
            using (TransactionScope ts = new TransactionScope())
            {
                try
                {
                    FengQiaoHelper ss = new FengQiaoHelper();
                    ss.SFOrder();
                    //if (insuranceWarrantyRecordExpressInfo.ID == 0)
                    //{
                    //    insuranceWarrantyRecordExpressInfo.CreatedTime = DateTime.Now;
                    //    insuranceWarrantyRecordExpressInfo.CreatedBy = CurrentUser.UserAccount;
                    //    insuranceWarrantyRecordExpressInfo.InsuranceWarrantyRecordID = insuranceWarrantyRecordId;
                    //    var entity = serv.AddEntities(insuranceWarrantyRecordExpressInfo);
                    //    responseResult.Success = entity.ID > 0;
                    //    //设置编码
                    //    if (responseResult.Success)
                    //    {
                    //        insuranceWarrantyRecordExpressInfo.ExpressNo =
                    //            CommonMethod.GenerateCode("SFAPI" + DateTime.Now.ToString("yyyyMMdd"),
                    //                insuranceWarrantyRecordExpressInfo.ID.ToString(), 3);
                    //        responseResult.Success = serv.UpdateEntities(entity);
                    //    }
                    //}
                    //else
                    //{
                    //    insuranceWarrantyRecordExpressInfo.ExpressNo =
                    //            CommonMethod.GenerateCode("SFAPI" + DateTime.Now.ToString("yyyyMMdd"),
                    //                insuranceWarrantyRecordExpressInfo.ID.ToString(), 3);
                    //    responseResult.Success = serv.UpdateEntities(insuranceWarrantyRecordExpressInfo);
                    //}
                    //if (responseResult.Success)
                    //{
                    //    var deliverInfo = new DeliverConsigneeInfo
                    //    {
                    //        company = insuranceWarrantyRecordExpressInfo.SendUserName,
                    //        contact = insuranceWarrantyRecordExpressInfo.SendUserName,
                    //        address = insuranceWarrantyRecordExpressInfo.SendUserAddress,
                    //        tel = insuranceWarrantyRecordExpressInfo.SendUserPhone,
                    //        province = insuranceWarrantyRecordExpressInfo.SendUserAddress.Split('省')[0] + "省",
                    //        city = insuranceWarrantyRecordExpressInfo.SendUserAddress.Split('省')[1].Split('市')[0] + "市",
                    //        county = insuranceWarrantyRecordExpressInfo.SendUserAddress.Split('市')[1],
                    //        mobile = insuranceWarrantyRecordExpressInfo.SendUserPhone,
                    //        shipperCode = ""
                    //    };
                    //    var consigneeInfo = new DeliverConsigneeInfo
                    //    {
                    //        company = insuranceWarrantyRecordExpressInfo.ReceiverUserName,
                    //        contact = insuranceWarrantyRecordExpressInfo.ReceiverUserName,
                    //        address = insuranceWarrantyRecordExpressInfo.ReceiverUserAddress,
                    //        tel = insuranceWarrantyRecordExpressInfo.ReceiverUserPhone,
                    //        province = insuranceWarrantyRecordExpressInfo.ReceiverUserAddress.Split('省')[0] + "省",
                    //        city =
                    //            insuranceWarrantyRecordExpressInfo.ReceiverUserAddress.Split('省')[1].Split('市')[0] + "市",
                    //        county = insuranceWarrantyRecordExpressInfo.ReceiverUserAddress.Split('市')[1],
                    //        mobile = insuranceWarrantyRecordExpressInfo.ReceiverUserPhone,
                    //        shipperCode = ""
                    //    };
                    //    var cargoInfo = new CargoInfo
                    //    {
                    //        cargo = "手机"
                    //    };
                    //    var orderInfo = new OrderInfoModel
                    //    {
                    //        orderId = insuranceWarrantyRecordExpressInfo.ExpressNo,
                    //        expressType = "1",
                    //        payMethod = 2,
                    //        isDocall = 2,
                    //        isGenBillNo = 1,
                    //        isGenEletricPic = 1,
                    //        payArea = "",
                    //        sendStartTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),
                    //        needReturnTrackingNo = "0",
                    //        deliverInfo = deliverInfo,
                    //        consigneeInfo = consigneeInfo,
                    //        cargoInfo = cargoInfo
                    //    };
                    //    ShunFengHelper sfhentity = new ShunFengHelper();
                    //    responseResult.Success = sfhentity.CreateOrder(orderInfo, DateTime.Now.ToString("yyyyMMddhhssmmffff"));
                    //    if(!responseResult.Success)
                    //    {
                    //     responseResult.Message = "下单失败，请稍后再试!";
                    //    }
                    //    else
                    //    {
                    //        responseResult.Message = "下单成功!";
                    //    }
                    //}
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    responseResult.Message = ex.Message;
                    ts.Dispose();
                }
            }
        }
        #endregion
        #region 获取快递单信息 GetShunFengOrder
        public JsonResult GetShunFengOrder(string orderId = "201404120000000000000001", string transMessageId = "201404120000000001")
        {
            ShunFengHelper sfhentity = new ShunFengHelper();
            var order = sfhentity.OrderQuery(orderId, transMessageId);
            return Json(new
            {
                success = true,
                ModelList = order
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion
	}
}